#include "SnakeWorld3.h"
#include "Result.h"
#include "snake.h"
#include <list>
#include "SimpleAudioEngine.h"
using namespace cocos2d;

CCScene* SnakeWorld3::scene()
{
	CCScene *scene = NULL;
	do
	{
		scene = CCScene::create();
		CC_BREAK_IF(!scene);
		SnakeWorld3 *layer = SnakeWorld3::create();
		CC_BREAK_IF(!layer);
		scene->addChild(layer);
	} while (0);
	return scene;
}

bool SnakeWorld3::init()
{
	bool bRet = false;
	do
	{
		CC_BREAK_IF(!CCLayer::init());

		CCSprite * g = CCSprite::create("grassback.png");
		CC_BREAK_IF(!g);
		g->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2,CCDirector::sharedDirector()->getWinSize().height/2));
		this->addChild(g,0);

		pPauseItem = CCMenuItemImage::create(
            "pause.png",
            "pause.png",
            this,
			menu_selector(SnakeWorld3::Pause));
		CC_BREAK_IF(!pPauseItem);
		pPauseItem->setPosition(ccp(-CCDirector::sharedDirector()->getWinSize().width/2 + 40,-CCDirector::sharedDirector()->getWinSize().height/2 + 40));
		ispause = false;

		CCMenu *pMenu = CCMenu::create(pPauseItem,NULL);
		CC_BREAK_IF(!pMenu);
		this->addChild(pMenu,3);

		
		//this->addChild(pStickMenu,3);

		stopsteps = 0;
		nostick = 0;


		newWorld = new world3();
		newWorld->putsnake(1,1,newWorld->getself());
		newWorld->putsupersnake(1,10,newWorld->getself());
		newWorld->putfood(newWorld->getself());
		newWorld->puttrap(newWorld->getself());
		this->addChild(newWorld,1);

		supersnake_speed = 1000;
		snake_speed = 800;
		gameTime = 600;
		
		
		//printf("%d",snake_speed);
		this->schedule(schedule_selector(SnakeWorld3::run),(float)snake_speed/1000);
		this->schedule(schedule_selector(SnakeWorld3::super_run),(float)supersnake_speed/1000);
		this->schedule(schedule_selector(SnakeWorld3::updateGameSpeed),0.1f);

		this->setTouchEnabled(true);

		extern bool Music_Enable;
		if(Music_Enable == true)
		{
			CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("backgroud.mp3",true);
		}
		
		
		bRet = true;
	} while (0);
	return bRet;
}

void SnakeWorld3::menuCloseCallBack(CCObject* pSender)
{
	//CCDirector::sharedDirector()->end();
	CCScene *s = Result::scene();
	CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1,s));
}

void SnakeWorld3::gameOver(CCObject* p)
{	
	this->removeAllChildren();
	this->unscheduleAllSelectors();
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("gameover.mp3");
	CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
	
	CCSprite * go = CCSprite::create("score.jpg");
	go->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2,CCDirector::sharedDirector()->getWinSize().height/2));
	this->addChild(go,2);

	CCLabelTTF *pNext = CCLabelTTF::create("NEXT","Segoe Marker",24);

	CCMenuItemLabel *pCloseItem = CCMenuItemLabel::create(pNext,
            this,
			menu_selector(SnakeWorld3::menuCloseCallBack));

    pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2,CCDirector::sharedDirector()->getWinSize().height/2 - 40));

    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
	this->addChild(pMenu,3);

	score = newWorld->getscore();
	char score_str[20] ={} ;	
	sprintf(score_str,"Score: %d",score);
	scoreTag = CCLabelTTF::create(score_str,"Segoe Marker",30);
	scoreTag->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2,CCDirector::sharedDirector()->getWinSize().height/2));
	this->addChild(scoreTag,3);
	
}

void SnakeWorld3::run(float dt)
{
	this->removeChild(scoreTag,true);
	score = newWorld->getscore();
	char score_str[20] ={} ;
	sprintf(score_str,"Score: %d",score);
	scoreTag = CCLabelTTF::create(score_str,"Segoe Marker",20);
	scoreTag->setPosition(ccp(700,440));
	this->addChild(scoreTag,1);
	if(newWorld->run(newWorld->getself()) == false)
	{
		gameOver(this);
	}
}
void SnakeWorld3::super_run(float dt)
{
	if(nostick == 0)
	{
		pStickItem = CCMenuItemImage::create(
            "stick.png",
            "stick.png",
            this,
			menu_selector(SnakeWorld3::sticksnake));
		pStickItem->setPosition(ccp(-CCDirector::sharedDirector()->getWinSize().width/2 + 40,CCDirector::sharedDirector()->getWinSize().height/2 - 40));
		sticked = false;
		pStickMenu = CCMenu::create(pStickItem,NULL);
		this->addChild(pStickMenu,3);
		nostick--;
	}
	else
	{
		nostick--;
	}

	if(stopsteps == 0)
	{
		sticked = false;
	}
	else
	{
		stopsteps--;
	}

	if(!sticked)
	{
		newWorld->super_run(newWorld->getself());
	}
}
void SnakeWorld3::ccTouchesEnded(CCSet* touches, CCEvent *event)
{
	CCTouch* touch = (CCTouch*)(touches->anyObject());

	CCPoint location = touch->getLocationInView();

	location = CCDirector::sharedDirector()->convertToGL(location);

	CCPoint head;
	note shead = newWorld->getsnakehead();
	head.x = shead.getx()*40+20;
	head.y = shead.gety()*40+20;

	char now_direction = newWorld->getdirection();
	char new_direction;

	if (now_direction == 'a' || now_direction == 'd')
	{
		if(location.y > head.y)
			new_direction = 'w';
		else
			new_direction = 's';
	}

	else if (now_direction == 'w' || now_direction == 's')
	{
		if(location.x > head.x)
			new_direction = 'd';
		else
			new_direction = 'a';
	}
	newWorld->setdirection(new_direction);
}

void SnakeWorld3::updateGameSpeed(float dt)
{
	if(newWorld->changeSpeed()){
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("eat.mp3");
	this->unschedule(schedule_selector(SnakeWorld3::run));
	//this->unschedule(schedule_selector(SnakeWorld::super_run));

	int snake_length = newWorld->getsnakelength();
	//int supersnake_length = newWorld->getsupersnakelength();
	if(snake_speed >= 200)
		snake_speed = 800 - snake_length * 40;
	//if(supersnake_speed >= 1000)
	//	supersnake_speed = 1000 - supersnake_length * 50;
	this->schedule(schedule_selector(SnakeWorld3::run),(float)snake_speed/1000);
	//this->schedule(schedule_selector(SnakeWorld::super_run),(float)supersnake_speed/1000);
	newWorld->resetchangeSpeed();

	}
	if(newWorld->changesuperSpeed()){
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("eat1.mp3");
	//this->unschedule(schedule_selector(SnakeWorld::run));
	this->unschedule(schedule_selector(SnakeWorld3::super_run));

	//int snake_length = newWorld->getsnakelength();
	int supersnake_length = newWorld->getsupersnakelength();
	//if(snake_speed >= 80)
	//	snake_speed = 800 - snake_length * 40;
	if(supersnake_speed >= 300)
		supersnake_speed = 1000 - supersnake_length * 50;
	//this->schedule(schedule_selector(SnakeWorld::run),(float)snake_speed/1000);
	this->schedule(schedule_selector(SnakeWorld3::super_run),(float)supersnake_speed/1000);
	newWorld->resetchangesuperSpeed();
	}

	this->removeChild(timeTag,true);
	char time_str[20] ={} ;
	sprintf(time_str,"TimeLeft: %02d:%02d",(int)gameTime/60,(int)gameTime%60);
	timeTag = CCLabelTTF::create(time_str,"Segoe Marker",20);
	timeTag->setPosition(ccp(700,400));
	this->addChild(timeTag,1);
	gameTime-=0.1;
	if(gameTime == 0)
	{
		gameOver(this);
	}
}

void SnakeWorld3::Pause(CCObject *p)
{
	if(ispause)
	{
		this->removeChild(pauseTag);
		CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
		ispause = false;	
		CCDirector::sharedDirector()->resume();
		CCMenuItemImage *pPauseItem = CCMenuItemImage::create(
            "pause.png",
            "pause.png",
            this,
			menu_selector(SnakeWorld::Pause));
		pPauseItem->setPosition(ccp(-CCDirector::sharedDirector()->getWinSize().width/2 + 40,-CCDirector::sharedDirector()->getWinSize().height/2 + 40));

		CCMenu *pMenu = CCMenu::create(pPauseItem,NULL);
		this->addChild(pMenu,3);
	}
	else
	{
		this->removeChild(pPauseItem);
		pauseTag = CCLabelTTF::create("PAUSE","Segoe Marker",100);
		pauseTag->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2,CCDirector::sharedDirector()->getWinSize().height/2));
		this->addChild(pauseTag,3);
		CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
		ispause = true;
		CCDirector::sharedDirector()->pause();

		CCMenuItemImage *pResumeItem = CCMenuItemImage::create(
            "resume.png",
            "resume.png",
            this,
			menu_selector(SnakeWorld::Pause));
		
		pResumeItem->setPosition(ccp(-CCDirector::sharedDirector()->getWinSize().width/2 + 40,-CCDirector::sharedDirector()->getWinSize().height/2 + 40));
		//ispause = false;

		CCMenu *pMenu1 = CCMenu::create(pResumeItem,NULL);
		//CC_BREAK_IF(!pMenu);
		this->addChild(pMenu1,3);
	}
}

void SnakeWorld3::sticksnake(CCObject *p)
{
	sticked = true;
	stopsteps = 4;
	nostick = 19;
	this->removeChild(pStickMenu);
}